package com.ysy.logistics.service.oam.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.oam.LoginLog;
import com.ysy.logistics.mapper.oam.LoginLogMapper;
import com.ysy.logistics.service.oam.LoginLogService;
import com.ysy.logistics.util.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/21 10:58
 **/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;
    @Override
    public Page<LoginLog> listByPage(PageInfo<LoginLog> pageInfo) {
        //获取参数
       log.debug("分页查询登录日志："+pageInfo);
        LocalDate beginTime = pageInfo.getBeginTime();
        LocalDate endTime = pageInfo.getEndTime();
        if (beginTime != null && endTime != null){
            LocalDate temp = beginTime;
           endTime = beginTime.isAfter(endTime)?beginTime:endTime;
           beginTime = temp;
        }
        LambdaQueryWrapper<LoginLog> lqw = new LambdaQueryWrapper();
        if (beginTime!=null){
            lqw.ge(LoginLog::getCreateTime,beginTime.atStartOfDay());
        }
        if (endTime!=null){
            //+1天
            endTime.plusDays(1);
            lqw.le(LoginLog::getCreateTime,endTime.atStartOfDay());
        }
        lqw.orderByDesc(LoginLog::getCreateTime);

        return loginLogMapper.selectPage(pageInfo.getPage(), lqw);
    }
}
